import { createAction } from 'redux-actions'
import { check, body, delJson, getJson, postJson, putJson, parseJSON, checkStatus, data } from './util'

export const fetchProfileTemplates = createAction('fetch profile templates', cb =>
	fetch('/profile-templates', getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(res => {
      cb && cb()
      return res.data
    })
)

export const fetchProfileTemplateFields = createAction('fetch profile template fields', () => 
	fetch(`/profile-templates/all-profile-fields`, getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
		.then(res => {
			return {
				...res.data,
			}
		})
)

export const addProfileTemplate = createAction('_', (displayName, cb) =>
	fetch('/profile-templates', {
		...postJson,
		...body({
			displayName
		})
	})
	.then(checkStatus)
	.then(parseJSON)
	.then(check)
  .then(cb)
)

export const deleteProfileTemplate = createAction('_', (profileTemplateId, cb) =>
	fetch(`/profile-templates/${profileTemplateId}`, delJson)
	.then(checkStatus)
	.then(parseJSON)
	.then(check)
	.then(cb)
)

export const requestAddProfileTemplate = createAction('request add profile template', displayName =>
	fetch('/profile-templates', {
		...postJson,
		...body({
			displayName
		})
	})
	.then(checkStatus)
	.then(parseJSON)
	.then(check)
	.then(res => res.data.idValue)
)

export const fetchProfileTemplate = createAction('fetch single profile template', profileTemplateId =>
	fetch(`/profile-templates/${profileTemplateId}`, getJson)
	.then(checkStatus)
	.then(parseJSON)
	.then(check)
	.then(data)
)

export const reorderProfileField = createAction('reorder profile field', (args) =>
	fetch(`/profile-templates/meta-profile-fields/reorder`, {
		...putJson,
		...body(args)
	})
	.then(checkStatus)
	.then(parseJSON)
	.then(check)
	.then(data)
)

export const sortProfileField = createAction('sort profile field')

export const addProfileField = createAction('_', (args, cb) => {
    const { profileTemplateId, ...others } = args
    return (
      fetch(`/profile-templates/meta-profile-fields`, {
        ...postJson,
        ...body(args)
      })
      .then(checkStatus)
      .then(parseJSON)
      .then(check)
      .then(res => {
        cb && cb()
        /*
        return {
          profileFieldId: res.data.idValue,
          isPublicVisibleEditable: 1,
          isMandatoryEditable: 1,
          isEnabledEditable: 1,
          ...args
        }
        */
      })
    )
  }
)

export const updateProfileTemplateField = createAction('update profile field', (args, cb) => {
    const { profileFieldId, ...others } = args
    return (
      fetch(`/profile-templates/meta-profile-fields/${profileFieldId}`, {
        ...putJson,
        ...body(others)
      })
      .then(checkStatus)
      .then(parseJSON)
      .then(check)
      .then(() => {
        cb && cb()
        return args
      })
    )
  }
)
export const updateProfileTemplateFieldCore = createAction('update profile field core', (args, cb) => {
    return (
      fetch(`/profile-templates/core-basic-profile-fields`, {
        ...putJson,
        ...body(args)
      })
      .then(checkStatus)
      .then(parseJSON)
      .then(check)
      .then(() => {
        cb && cb()
        return args
      })
    )
  }
)

export const deleteProfileTemplateField = createAction('delete profile field', (args, cb) =>
	fetch(`/profile-templates/meta-profile-fields/${args.profileFieldId}`, {
    ...delJson,
    ...body({
      dataType: args.dataType
    })
  })
	.then(checkStatus)
	.then(parseJSON)
	.then(check)
  .then(() => {
    cb && cb()
    return args
  })
)

export const fetchOrg = createAction('fetch org', () =>
	fetch('/org', getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
		.then(data)
)

export const updateOrg = createAction('update org', (arg, cb) =>
	fetch('/org', {
		...putJson,
		...body(arg)
	})
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
		.then(cb)
)

export const syncOrg = createAction('sync org')

export const resetOrg = createAction('reset org')

export const fetchRolePermissions = createAction('fetch role permissions', () => 
	fetch('/role-permissions/config-setup', getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
		.then(data)
)

export 
const syncRolePermissons = createAction('sync role permissions')

export 
const updateRolePermissions = createAction('update role permissions', (arg, cb) =>
	fetch('/role-permissions/config-setup', {
		...putJson,
		...body(arg)
	})
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
		.then(cb)
)

export
const manualHandleCSV = createAction('_', (documentId, cb) =>
  fetch(`/onboarding-flows/profile-templates-csv-files/manual-operation/${documentId}`, putJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
		.then(cb)
)

export
const resetProfileTemplate = createAction('reset profile template')

export
const fetchRosterAvailableFields = createAction('fetch roster available fields', cb =>
  fetch('/users/roster/available-fields', getJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(res => {
      cb && cb();
      return res.data
    })
)

export
const manualHandleRosterCSV = createAction('_', (documentId, cb) =>
  fetch(`/users/roster/manual-operation/${documentId}`, putJson)
		.then(checkStatus)
		.then(parseJSON)
		.then(check)
    .then(cb)
)

export
const resetSystem = createAction('reset system');
